package opt.power.springdemo.user.mapper;

import opt.power.springdemo.user.mapper.model.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author xuliduo
 * @date 2017/11/16
 * @description interface UserMapper
 */
@Mapper
public interface UserMapper {

    /**
     * 获得所有用户
     *
     * @return users
     */
    @Select("SELECT * FROM t_user")
    @Results({
            @Result(property = "name", column = "user_name"),
            @Result(property = "password", column = "user_pwd")
    })
    List<User> getAll();

    /**
     * 通过id 获得用户
     *
     * @param id user's id
     * @return user
     */
    @Select("SELECT * FROM t_user WHERE id = #{id}")
    @Results({
            @Result(property = "name", column = "user_name"),
            @Result(property = "password", column = "user_pwd")
    })
    User findById(Long id);

    @Insert("INSERT INTO t_user(user_name,user_pwd) VALUES(#{name}, #{password})")
    void insert(User user);

    @Update("UPDATE t_user SET user_name=#{name},user_pwd=#{password} WHERE id =#{id}")
    void update(User user);

    @Delete("DELETE FROM t_user WHERE id =#{id}")
    void delete(Long id);

    @Select("SELECT * FROM t_user WHERE user_name = #{name} AND user_pwd = #{password}")
    @Results({
            @Result(property = "name", column = "user_name"),
            @Result(property = "password", column = "user_pwd")
    })
    List<User> findByNameAndPassword(User user);
}
